package chooongg.base.utils

import android.app.Activity
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.annotation.AnimRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityOptionsCompat
import androidx.fragment.app.Fragment
import chooongg.base.Utils
import chooongg.base.annotation.TranslucentStatusBar

/**
 * 活动工具类
 */

inline fun <reified T : Activity> Context.startActivity() {
    startActivity(Intent(this, T::class.java).apply {
        if (this !is Activity) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    })
}

inline fun <reified T : Activity> Context.startActivity(options: Bundle) {
    startActivity(Intent(this, T::class.java).apply {
        if (this !is Activity) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    }, options)
}

inline fun <reified T : Activity> Context.startActivity(
    @AnimRes enterAnim: Int,
    @AnimRes exitAnim: Int
) {
    val options = ActivityOptionsCompat.makeCustomAnimation(this, enterAnim, exitAnim).toBundle()
    startActivity(Intent(this, T::class.java).apply {
        if (this !is Activity) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    }, options)
}

inline fun <reified T : Activity> Context.startActivity(extras: Intent.() -> Unit) {
    startActivity(Intent(this, T::class.java).apply {
        if (this !is Activity) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        extras()
    })
}

inline fun <reified T : Activity> Context.startActivity(
    options: Bundle,
    extras: Intent.() -> Unit
) {
    startActivity(Intent(this, T::class.java).apply {
        if (this !is Activity) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        extras()
    }, options)
}

inline fun <reified T : Activity> Context.startActivity(
    @AnimRes enterAnim: Int,
    @AnimRes exitAnim: Int,
    extras: Intent.() -> Unit
) {
    val options = ActivityOptionsCompat.makeCustomAnimation(this, enterAnim, exitAnim).toBundle()
    startActivity(Intent(this, T::class.java).apply {
        if (this !is Activity) addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        extras()
    }, options)
}

inline fun <reified T : Activity> Activity.startActivityForResult(requestCode: Int) {
    startActivityForResult(Intent(this, T::class.java), requestCode)
}

inline fun <reified T : Activity> Activity.startActivityForResult(
    options: Bundle,
    requestCode: Int
) {
    startActivityForResult(Intent(this, T::class.java), requestCode, options)
}

inline fun <reified T : Activity> Activity.startActivityForResult(
    @AnimRes enterAnim: Int,
    @AnimRes exitAnim: Int, requestCode: Int
) {
    val options = ActivityOptionsCompat.makeCustomAnimation(this, enterAnim, exitAnim).toBundle()
    startActivityForResult(Intent(this, T::class.java), requestCode, options)
}

inline fun <reified T : Activity> Activity.startActivityForResult(
    extras: Intent.() -> Unit,
    requestCode: Int
) {
    startActivityForResult(Intent(this, T::class.java).apply(extras), requestCode)
}

inline fun <reified T : Activity> Activity.startActivityForResult(
    options: Bundle,
    extras: Intent.() -> Unit,
    requestCode: Int
) {
    startActivityForResult(Intent(this, T::class.java).apply(extras), requestCode, options)
}

inline fun <reified T : Activity> Activity.startActivityForResult(
    @AnimRes enterAnim: Int,
    @AnimRes exitAnim: Int,
    extras: Intent.() -> Unit,
    requestCode: Int
) {
    val options = ActivityOptionsCompat.makeCustomAnimation(this, enterAnim, exitAnim).toBundle()
    startActivityForResult(Intent(this, T::class.java).apply(extras), requestCode, options)
}


inline fun <reified T : Activity> Fragment.startActivity() {
    startActivity(Intent(context, T::class.java))
}

inline fun <reified T : Activity> Fragment.startActivity(options: Bundle) {
    startActivity(Intent(context, T::class.java), options)
}

inline fun <reified T : Activity> Fragment.startActivity(
    @AnimRes enterAnim: Int,
    @AnimRes exitAnim: Int
) {
    val options =
        ActivityOptionsCompat.makeCustomAnimation(context!!, enterAnim, exitAnim).toBundle()
    startActivity(Intent(context, T::class.java), options)
}

inline fun <reified T : Activity> Fragment.startActivity(extras: Intent.() -> Unit) {
    startActivity(Intent(context, T::class.java).apply(extras))
}

inline fun <reified T : Activity> Fragment.startActivity(
    options: Bundle,
    extras: Intent.() -> Unit
) {
    startActivity(Intent(context, T::class.java).apply(extras), options)
}

inline fun <reified T : Activity> Fragment.startActivity(
    @AnimRes enterAnim: Int,
    @AnimRes exitAnim: Int,
    extras: Intent.() -> Unit
) {
    val options =
        ActivityOptionsCompat.makeCustomAnimation(context!!, enterAnim, exitAnim).toBundle()
    startActivity(Intent(context, T::class.java).apply(extras), options)
}

inline fun <reified T : Activity> Fragment.startActivityForResult(requestCode: Int) {
    startActivityForResult(Intent(context, T::class.java), requestCode)
}

inline fun <reified T : Activity> Fragment.startActivityForResult(
    options: Bundle,
    requestCode: Int
) {
    startActivityForResult(Intent(context, T::class.java), requestCode, options)
}

inline fun <reified T : Activity> Fragment.startActivityForResult(
    @AnimRes enterAnim: Int,
    @AnimRes exitAnim: Int, requestCode: Int
) {
    val options =
        ActivityOptionsCompat.makeCustomAnimation(context!!, enterAnim, exitAnim).toBundle()
    startActivityForResult(Intent(context, T::class.java), requestCode, options)
}

inline fun <reified T : Activity> Fragment.startActivityForResult(
    extras: Intent.() -> Unit,
    requestCode: Int
) {
    startActivityForResult(Intent(context, T::class.java).apply(extras), requestCode)
}

inline fun <reified T : Activity> Fragment.startActivityForResult(
    options: Bundle,
    extras: Intent.() -> Unit,
    requestCode: Int
) {
    startActivityForResult(Intent(context, T::class.java).apply(extras), requestCode, options)
}

inline fun <reified T : Activity> Fragment.startActivityForResult(
    @AnimRes enterAnim: Int,
    @AnimRes exitAnim: Int,
    extras: Intent.() -> Unit,
    requestCode: Int
) {
    val options =
        ActivityOptionsCompat.makeCustomAnimation(context!!, enterAnim, exitAnim).toBundle()
    startActivityForResult(Intent(context, T::class.java).apply(extras), requestCode, options)
}


fun getLauncherActivity(packageName: String? = Utils.app.packageName): String? {
    if (packageName.isSpace()) return null
    val intent = Intent(Intent.ACTION_MAIN)
    intent.addCategory(Intent.CATEGORY_LAUNCHER)
    intent.`package` = packageName
    val info = Utils.app.packageManager.queryIntentActivities(intent, 0)
    if (info.size == 0) return null
    return info[0].activityInfo.name
}

fun Context.loadLabel() = if (this is Activity) {
    val activityInfo = packageManager.getActivityInfo(ComponentName(this, javaClass), 0)
    activityInfo.loadLabel(packageManager)
} else null

fun AppCompatActivity.isTranslucentStatusBar() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
        && javaClass.isAnnotationPresent(TranslucentStatusBar::class.java)
        && supportActionBar == null